﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Mvc4_CRUD_jQueryDialog.Models;

namespace Mvc4_CRUD_jQueryDialog.Controllers
{
    public class HomeController : Controller
    {
        ModelServices mobjModel = new ModelServices();

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Demo()
        {
            Response.Redirect("http://www.dotnet-tricks.com/Tutorial/mvclist");
            return View();
        }

        //Bind Web Grid and also do paging
        public ActionResult WebGrid(int page = 1, string sort = "name", string sortDir = "ASC")
        {
            const int pageSize = 5;
            var totalRows = mobjModel.CountCustomer();

            sortDir = sortDir.Equals("desc", StringComparison.CurrentCultureIgnoreCase) ? sortDir : "asc";

            var validColumns = new[] { "custid", "name", "address", "contactno" };
            if (!validColumns.Any(c => c.Equals(sort, StringComparison.CurrentCultureIgnoreCase)))
                sort = "custid";

            var customer = mobjModel.GetCustomerPage(page, pageSize, "it." + sort + " " + sortDir);

            var data = new PagedCustomerModel()
            {
                TotalRows = totalRows,
                PageSize = pageSize,
                Customer = customer
            };
            return View(data);
        }


        public ActionResult Create()
        {
            if (Request.IsAjaxRequest())
            {

                ViewBag.IsUpdate = false;
                return View("_Customer");
            }
            else

                return View();
        }

        public ActionResult View(int id)
        {
            var data = mobjModel.GetCustomer(id);
            if (Request.IsAjaxRequest())
            {
                CustomerModel cust = new CustomerModel();
                cust.CustID = data.CustID;
                cust.Name = data.Name;
                cust.Address = data.Address;
                cust.ContactNo = data.ContactNo;

                return View("_ViewCustomer", cust);
            }
            else

                return View(data);
        }

        public ActionResult Edit(int id)
        {
            var data = mobjModel.GetCustomer(id);

            if (Request.IsAjaxRequest())
            {
                CustomerModel cust = new CustomerModel();
                cust.CustID = data.CustID;
                cust.Name = data.Name;
                cust.Address = data.Address;
                cust.ContactNo = data.ContactNo;

                ViewBag.IsUpdate = true;
                return View("_Customer", cust);
            }
            else

                return View(data);
        }

        public ActionResult Delete(int id)
        {
            bool check = mobjModel.DeleteCustomer(id);
            var data = mobjModel.GetCustomer();
            return RedirectToAction("WebGrid");
        }

        [HttpPost]
        public ActionResult CreateEditCustomer(CustomerModel mCust, string Command)
        {
            // Validate the model being submitted

            if (!ModelState.IsValid)
            {

                return PartialView("_Customer", mCust);

            }

            else if (Command == "Save")
            {
                Customer mobjcust = new Customer();
                mobjcust.CustID = mCust.CustID;
                mobjcust.Address = mCust.Address;
                mobjcust.ContactNo = mCust.ContactNo;
                mobjcust.Name = mCust.Name;

                bool check = mobjModel.CreateCustomer(mobjcust);
                if (check)
                {
                    TempData["Msg"] = "Data has been saved succeessfully";
                    ModelState.Clear();
                    return RedirectToAction("WebGrid", "Home");
                }
            }

            else if (Command == "Update")
            {
                Customer mobjcust = new Customer();
                mobjcust.CustID = mCust.CustID;
                mobjcust.Address = mCust.Address;
                mobjcust.ContactNo = mCust.ContactNo;
                mobjcust.Name = mCust.Name;

                bool check = mobjModel.UpdateCustomer(mobjcust);
                if (check)
                {
                    TempData["Msg"] = "Data has been updated succeessfully";
                    ModelState.Clear();
                    return RedirectToAction("WebGrid", "Home");
                }

            }

            return PartialView("_Customer");
        }
    }
}
